package com.example.spsaveqqty;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.spsaveqqty.guides.Guide;
import com.example.spsaveqqty.userManage.UserDao;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class PersonalGuideDetailActivity extends AppCompatActivity implements View.OnClickListener, OnBannerListener {

    private Banner mBanner;
    private ImageView img_userIcon;//头像
    private TextView tv_del_guide;//删除按钮
    private TextView tv_username_detail;//昵称
    private TextView tv_guide_title_detail;//旅游攻略 标题
    private TextView tv_guide_content_detail;//旅游攻略 内容
    private TextView tv_guide_place_name_detail;//旅游攻略 地区
    private TextView tv_guide_time_detail;//旅游攻略 时间
    Guide guide;
    private UserDao userdao;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_guide_detail);
        requestWritePermission();
        //创建userdao对象
        userdao = new UserDao(this);
        init();
    }
    private void init(){
        mBanner = findViewById(R.id.banner_guides);
        img_userIcon = findViewById(R.id.img_userIcon);
        tv_del_guide =  findViewById(R.id.tv_del_guide);
        tv_username_detail = findViewById(R.id.tv_username_detail);
        tv_guide_title_detail = findViewById(R.id.tv_guide_title_detail);
        tv_guide_content_detail = findViewById(R.id.tv_guide_content_detail);
        tv_guide_place_name_detail = findViewById(R.id.tv_guide_place_name_detail);
        tv_guide_time_detail = findViewById(R.id.tv_guide_time_detail);
        //设置删除的点击事件
        tv_del_guide.setOnClickListener(this);
        tv_guide_place_name_detail.setOnClickListener(this);
        //初始化值：
        Intent intent = getIntent();
        guide = (Guide)intent.getSerializableExtra("guide");

        //图片资源
        List<String> imgeList = new ArrayList<>();
        for (int i = 0; i < guide.getPicture().length; i++) {
            imgeList.add(guide.getPicture()[i]);//把图片资源循环放入list里面
            //设置图片加载器，通过Glide加载图片
            mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(PersonalGuideDetailActivity.this).load(path).into(imageView);
                }
            });
            //设置轮播的动画效果,里面有很多种特效,可以到GitHub上查看文档。
            mBanner.setBannerAnimation(Transformer.Default);
            mBanner.setImages(imgeList);//设置图片资源
            mBanner.setBannerStyle(BannerConfig.NUM_INDICATOR);//设置banner显示样式（显示数字指示器）
            //设置指示器位置（即图片下面的那个小圆点）
            mBanner.setIndicatorGravity(BannerConfig.CENTER);
            mBanner.setDelayTime(2000);//设置轮播时间2秒切换下一图
            mBanner.setOnBannerListener(this);//设置监听
            mBanner.start();//开始进行banner渲染
        }

        if (!TextUtils.isEmpty(guide.getProducer_image())){//代表用户有上传过头像
            if (guide.getIconFlag().equals("1")){
                Bitmap bitmapIcon = BitmapFactory.decodeFile(guide.getProducer_image());
                bitmapIcon = Bitmap.createScaledBitmap(bitmapIcon,60,60,true);
                img_userIcon.setImageBitmap(bitmapIcon);
            }else if (guide.getIconFlag().equals("2")){
                Uri imgUri = Uri.parse(guide.getProducer_image());
                try {
                    Bitmap  bitmapIcon2 = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri));
                    bitmapIcon2 = Bitmap.createScaledBitmap(bitmapIcon2,60,60,true);
                    img_userIcon.setImageBitmap(bitmapIcon2);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
        tv_username_detail.setText(guide.getProducer_name());
        tv_guide_title_detail.setText(guide.getTitle());
        tv_guide_content_detail.setText("\t\t\t\t"+guide.getConent());
        tv_guide_content_detail.setMovementMethod(ScrollingMovementMethod.getInstance());//内容过多时显示滚动条可以上下滑动查看
        tv_guide_place_name_detail.setText(guide.getPlace_name());
        tv_guide_time_detail.setText(guide.getDatetime());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_del_guide://点击删除
                userdao.delPersonalGuide(guide.getStrategy_id());
                //返回个人中心进行刷新：
                Intent intent = new Intent(PersonalGuideDetailActivity.this, PersonalCenterActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_guide_place_name_detail://点击地区 要跳转到该地区的地图天气页面
                //跳转到地图天气模块：
                Intent intent2 = new Intent(PersonalGuideDetailActivity.this, map_weather.class);
                intent2.putExtra("position_name",guide.getPlace_name());
                startActivity(intent2);
                finish();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBanner.startAutoPlay();//开始轮播
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBanner.stopAutoPlay();//结束轮播
    }

    //对轮播图设置点击监听事件
    @Override
    public void OnBannerClick(int position) {
    }

    //动态申请读取手机内存的权限：
    private void requestWritePermission(){
        //  动态申请WRITE_EXTERNAL_STORAGE 这个危险权限。因为相册中的照片时存储在SD卡上的，我们从SD卡中读取照片就需要申请这个权限
        //  WRITE_EXTERNAL_STORAGE  ——  同时授权程序对SD卡读和写的能力
        if (ActivityCompat.checkSelfPermission(PersonalGuideDetailActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(PersonalGuideDetailActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }
}
